//
//  ClassView.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ClassView.h"

#import "LRAFNetwork.h"
#import <AFNetworking.h>
#import "ShipuModel.h"
#import <UIImageView+WebCache.h>
#import "ClassCollectionViewCell.h"
#import "MyHeadCollectionViewCell.h"
@implementation ClassView
//- (void)dealloc
//{
//    [super dealloc];
//}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if ( self) {
        
        [self data];
        [self creatClassView];
       
    }
    return self;
}

-(void)data
{
    NSString *str = @"http://api.izhangchu.com/?methodName=CategoryIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = responseObject[@"data"][@"data"];
        
        self.classArr= [ShipuModel modelHanderWithArray:arr];
        self.allTextArr = [ NSMutableArray array];
        for (NSDictionary *dic1 in arr) {
            //获取第二个data
            NSArray *iArr = dic1[@"data"];
            ShipuModel  *model = [[ShipuModel alloc] init];
            model.textArr = [ NSMutableArray array];
            //把所有的文字存成数组 再添加到大数组里
            for (NSDictionary *dic2 in iArr) {
                [model.textArr addObject:dic2[@"text"]];
            }
            [self.allTextArr addObject: model.textArr];
            
        }
        NSLog(@"~~~~~~!!!!!%@",_classArr);
        NSLog(@"~~~~~~~~~~~~~~~~~~%@",_allTextArr);
        //NSLog(@"~~~~~%@",_tArr);
        [_classCollView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
    
    
}
-(void)creatClassView
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    //cell大小
    layout.itemSize = CGSizeMake((WIDTH*375 - 20) /4 ,HEIGHT*30);
    //行列间距
    //行
    layout.minimumLineSpacing = 1 * WIDTH;
    //列
    layout.minimumInteritemSpacing = 0;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(0,WIDTH*10,0,0);
    _classCollView =[[ UICollectionView alloc] initWithFrame:CGRectMake(375 * 1 *WIDTH, 0, WIDTH*375, 667 * HEIGHT)collectionViewLayout:layout];
    _classCollView .delegate = self;
    _classCollView.dataSource = self;
    _classCollView.backgroundColor = [UIColor greenColor];
   _classCollView.showsHorizontalScrollIndicator = NO;
    //[_collectionView release];
    _classCollView.pagingEnabled = NO;
    [_classCollView registerClass:[ClassCollectionViewCell  class] forCellWithReuseIdentifier:@"cell1"];
    //头部区域
    [_classCollView registerClass: [MyHeadCollectionViewCell class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"header1"];
    
    layout.headerReferenceSize = CGSizeMake(100, 50 *HEIGHT);
    [self addSubview:_classCollView];
    
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
           MyHeadCollectionViewCell *headercell1 =[ collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header1" forIndexPath:indexPath];

        ShipuModel *m = self.classArr[indexPath.section];
        headercell1.title.text = m.text;
    
        return headercell1;
        
  
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return _classArr.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{ //获取对应的每一个数组
    _textArr = _allTextArr[ section];
    
    return _textArr.count ;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
             ClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        
        _listArr = _allTextArr[indexPath.section];
        cell.name.text = _listArr[indexPath.item];
   
    return cell;
   }


@end
